<?php
/**
 * Joel优化JSSDK
 */

namespace Joel\wx;
class Jssdk
{
	private $appid;
	private $token;//access_token
	private $url;	

	public function __construct($options)
	{
		$this->appid = isset($options['appid'])?$options['appid']:'';
		$this->token = isset($options['token'])?$options['token']:'';
		$this->url = isset($options['url'])?$options['url']:'';
	}
	
	public function getSignPackage() {
	    $appid=$this->appid;
		$token=$this->token;
		$url=$this->url;
		if(!$appid || !$token || !$url){
			return FALSE;
		}
		//处理超链接#	
		$ret = strpos($url,'#');
	    if ($ret){
	    	$url = substr($url,0,$ret);
	    }	        
	    $url = trim($url);
	    $jsapiTicket = $this->getJsApiTicket($token);
	    $timestamp = time();
	    $nonceStr = $this->createNonceStr();
	
	    // 这里参数的顺序要按照 key 值 ASCII 码升序排序
	    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
	
	    $signature = sha1($string);
	
	    $signPackage = array(
	      "appId"     => $appid,
	      "nonceStr"  => $nonceStr,
	      "timestamp" => $timestamp,
	      "url"       => $url,
	      "signature" => $signature,
	      "rawString" => $string
	    );
	    return $signPackage; 
  }
	private function createNonceStr($length = 16) {
    	$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    	$str = "";
    	for ($i = 0; $i < $length; $i++) {
      		$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    	}
    	return $str;
  	}
	
	private function getJsApiTicket($accessToken) {
	    // jsapi_ticket 应该全局存储与更新，以下代码以写入到文件中做示例
	    $data = json_decode(file_get_contents("jsapi_ticket.json"));
		//dump($data);
		if ($data->expire_time < time()) {
	     // $accessToken = $this->getAccessToken();
	      $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
		  $res = json_decode($this->http_get($url));
	      //dump($res);die;
	      $ticket = $res->ticket;
	      if ($ticket) {
	        $data->expire_time = time() + 7000;
	        $data->jsapi_ticket = $ticket;
			file_put_contents("jsapi_ticket.json",json_encode($data));
	        $fp = fopen("jsapi_ticket.json", "w");
	        fwrite($fp, json_encode($data));
	        fclose($fp);
	      }else{
	      	die('no ticket!');
	      }
	    } else {
	      $ticket = $data->jsapi_ticket;
	    }

    return $ticket;
  }
	
	/**
	 * GET 请求
	 * @param string $url
	 */
	private function http_get($url){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
	}
	
}
